package lanqiao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class 数组切分 {
    static class Node{
        public int value;
        public ArrayList<Integer> nexts;

        public Node(int value, ArrayList<Integer> list) {
            this.value = value;
            this.nexts = list;
        }
    }
    public static void main(String[] args) {
        int[] arr = {1,3,2,4};
        HashMap<Integer, Node> hash = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            ArrayList<Integer> list = new ArrayList<>();
            list.add(arr[i]);
            if (i>0){
                list.add(arr[i-1]);
            }
            hash.put(arr[i],new Node(arr[i],list));

        }
        int count = 1;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if (hash.get(arr[i]).value == arr[j]-1 || hash.get(arr[i]).value == arr[j]+1 || hash.get(arr[i]).nexts.contains(arr[j]-1) || hash.get(arr[i]).nexts.contains(arr[j]+1)){
                    hash.get(arr[i]).nexts.add(arr[j]);
                    count++;
                }else {
                    break;
                }
            }
        }
        if (hash.get(arr[arr.length-2]).nexts.size()+1==arr.length){
            count++;
        }
        System.out.println(count);
    }
}
